package com.gaga.console.core.service.impl;

import cn.hutool.core.lang.UUID;
import com.gaga.common.entity.file.FastDFSFile;
import com.gaga.common.entity.file.FileDesc;
import com.gaga.common.utils.FileManageUtils;
import com.gaga.console.core.service.FileManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @author liujia
 */
@Slf4j
@Service
public class FileManageServiceImpl implements FileManageService {

    @Override
    public FileDesc saveFile(MultipartFile multipartFile, FileDesc fileDesc) throws Exception {
        String fileName = multipartFile.getOriginalFilename();
        String ext = fileDesc.getFileName().substring(fileDesc.getFileName().lastIndexOf(".") + 1);
        byte[] file_buff = null;
        InputStream inputStream = multipartFile.getInputStream();

        if (inputStream != null) {
            int len1 = inputStream.available();
            file_buff = new byte[len1];
            inputStream.read(file_buff);
        }
        String md5File = UUID.fastUUID().toString(true);
        inputStream.close();

        FastDFSFile fastDFSFile = new FastDFSFile(fileName, file_buff, ext, md5File);
        return FileManageUtils.upload(fastDFSFile, fileDesc);
    }
}
